package com.newthread.jdp.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.newthread.jdp.model.Message;
import com.newthread.jdp.model.MessageState;
import com.newthread.jdp.model.Tag;
import com.newthread.jdp.model.UserInfo;
import com.newthread.jdp.service.MessageService;
import com.newthread.jdp.service.PushService;
import com.newthread.jdp.service.TagService;
import com.newthread.jdp.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

@Component
@Scope("prototype")
public class PushAction extends ActionSupport implements SessionAware {
	private static final long serialVersionUID = 1L;
	private String resultCode;
	private Map<String, Object> session;
	private UserService userService;
	private PushService pushService;
	private TagService tagService;
	private MessageService messageService;
	private String pushedIds;
	private String title, content;
	private String messageState;
	private int messageType;
	private String pushedTag;
	private String querryOnlineId;
	private int messageId;

	public String queryOnlineStatus() {
		if (!(boolean) (session.get("login"))) {
			this.setResultCode("fail user not login");
			return ERROR;
		}
		UserInfo userTemp = new UserInfo(querryOnlineId);
		UserInfo userInfo = (UserInfo) userService.querry(userTemp).get(0);
		this.setResultCode(pushService.queryBindList(userInfo));
		return SUCCESS;
	}

	public String pushMessage() {
		if (!(boolean) (session.get("login"))) {
			this.setResultCode("fail user not login");
			return ERROR;
		}
		UserInfo userTemp = new UserInfo((String) session.get("user"));
		UserInfo userInfo = userService.querry(userTemp).get(0);
		List<UserInfo> reciveUsers = new ArrayList<UserInfo>();
		String[] pushedId = pushedIds.split(",");
		for (String string : pushedId) {
			UserInfo pushedUser = userService.querry(new UserInfo(string)).get(0);
			reciveUsers.add(pushedUser);
		}
		Message msg = new Message(userInfo, reciveUsers, title, content,MessageState.valueOf(messageState), messageType);
		messageService.saveMessage(msg);
//		updatePushUser(userInfo, msg);
		pushService.pushMessage(reciveUsers, msg);
//		for (UserInfo pushedUser : reciveUsers) {
//			updateReceveUser(pushedUser, msg);
//		}
		this.setMessageId(msg.getId());
		this.setResultCode("sucess");
		return SUCCESS;
	}

	public String pushTagMessage() {
		if (!(boolean) (session.get("login"))) {
			this.setResultCode("fail user not login");
			return ERROR;
		}
		UserInfo userTemp = new UserInfo((String) session.get("user"));
		UserInfo userInfo = userService.querry(userTemp).get(0);
		List<UserInfo> pushedUsers = tagService.querryByTag(new Tag(pushedTag)).get(0).getUsers();
		Message msg = new Message(userInfo, pushedUsers, title, content,
				MessageState.valueOf(messageState), messageType);
		messageService.saveMessage(msg);
//		updatePushUser(userInfo, msg);
		pushService.pushMessage(pushedUsers, msg);
//		for (UserInfo pushedUser : pushedUsers) {
//			updateReceveUser(pushedUser, msg);
//		}
		this.setMessageId(msg.getId());
		this.setResultCode("sucess");
		return SUCCESS;
	}

	public String pushReplyMessage() {
		if (!(boolean) (session.get("login"))) {
			this.setResultCode("fail user not login");
			return ERROR;
		}
		Message fatherMsg = messageService.querryMessageById(messageId);
		UserInfo userTemp = new UserInfo((String) session.get("user"));
		UserInfo userInfo = userService.getUser(userTemp);
		List<UserInfo> pushedUsers = new ArrayList<UserInfo>();
		pushedUsers.add(fatherMsg.getPushUser());
		Message sonMsg = new Message(userInfo, pushedUsers, title, content,
				MessageState.valueOf(messageState), messageType);
		messageService.saveMessage(sonMsg);
//		updatePushUser(userInfo, sonMsg);
		List<Message> msgs = fatherMsg.getMessages();
		if (msgs.size() == 0) {
			msgs = new ArrayList<Message>();
		}
		msgs.add(sonMsg);
		fatherMsg.setMessages(msgs);
		messageService.updateMessage(fatherMsg);
		pushService.pushMessage(pushedUsers, sonMsg);
		this.setMessageId(sonMsg.getId());
		this.setResultCode("sucess");
		return SUCCESS;
	}

	public String pushBroadcastMessage() {
		if (!(boolean) (session.get("login"))) {
			this.setResultCode("fail user not login");
			return ERROR;
		}
		UserInfo userTemp = new UserInfo((String) session.get("user"));
		UserInfo userInfo = userService.getUser(userTemp);
		List<UserInfo> pushedUsers = userService.querryAll();
		pushedUsers.remove(userInfo);
		Message msg = new Message(userInfo, pushedUsers, title, content,MessageState.valueOf(messageState), messageType);
		messageService.saveMessage(msg);
//		updatePushUser(userInfo, msg);
		pushService.pushMessage(pushedUsers, msg);
//		for (UserInfo pushedUser : pushedUsers) {
//			updateReceveUser(pushedUser, msg);
//		}
		this.setMessageId(msg.getId());
		this.setResultCode("sucess");
		return SUCCESS;
	}

//	private void updateReceveUser(UserInfo userInfo, Message receveMessage) {
//		List<Message> msgs= userInfo.getReceveMessage();
//		if(msgs==null){
//			msgs=new ArrayList<Message>();
//		}
//		msgs.add(receveMessage);
//		userInfo.setReceveMessage(msgs);
//		userService.update(userInfo);
//	}

//	private void updatePushUser(UserInfo userInfo, Message msg) {
//		List<Message> pushMessages = userInfo.getPushMessage();
//		if (pushMessages==null) {
//			pushMessages = new ArrayList<Message>();
//		}
//		pushMessages.add(msg);
//		userInfo.setPushMessage(pushMessages);
//		userService.update(userInfo);
//	}

	public String getPushedIds() {
		return pushedIds;
	}

	public void setPushedIds(String pushedIds) {
		this.pushedIds = pushedIds;
	}

	public String getTitle() {
		return title;
	}

	public String getPushedTag() {
		return pushedTag;
	}

	public void setPushedTag(String pushedTag) {
		this.pushedTag = pushedTag;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getMessageState() {
		return messageState;
	}

	public void setMessageState(String messageState) {
		this.messageState = messageState;
	}

	public int getMessageType() {
		return messageType;
	}

	public void setMessageType(int messageType) {
		this.messageType = messageType;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public String getResultCode() {
		return resultCode;
	}

	public String getQuerryOnlineId() {
		return querryOnlineId;
	}

	public void setQuerryOnlineId(String querryOnlineId) {
		this.querryOnlineId = querryOnlineId;
	}

	public void setResultCode(String resultCode) {
		this.resultCode = resultCode;
	}

	public int getMessageId() {
		return messageId;
	}

	public void setMessageId(int messageId) {
		this.messageId = messageId;
	}

	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@Resource
	public void setPushService(PushService pushService) {
		this.pushService = pushService;
	}

	@Resource
	public void setTagService(TagService tagService) {
		this.tagService = tagService;
	}

	@Resource
	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}
}
